<?php
session_start();
include 'config.php';

$bagian 	= getBagian();
$subbagian 	= getSubBagian($bagian);

if( !isLogged() ){
    header('Location:login.php');
}
elseif( isValidUser($subbagian) ){
    // user page
    include 'library/model.php';
    $action		= isset($_GET['action']) ? $_GET['action'] : 'entry';
    $model 		= getModel($subbagian, $db);
    
    if( !in_array($action, array('edit', 'confirm_edit', 'confirmed')) ){
        clearEditSessionState();
    }
    
    switch( $action ){
        case 'delete':
            if( isCanDelete() ){
                $id = (int)$_GET['id'];
                $model->delete($id);
                $page = sprintf('page/view/%s/%s.php', $bagian, $subbagian);
            }
            else{
                $page = 'page/error/cant_delete.php';
            }
            break;
        
        case 'edit':
            if( isCanEdit() ){
                $id      = (int)$_GET['id'];
                
                $data = $model->getById($id);
                if( $data != NULL ){
                    // id ybs diletakkan pad session
                    $_SESSION['edit_id'] = $id;
                    $page = sprintf('page/edit/%s/%s.php', $bagian, $subbagian);
                }
                else{
                    refresh('Data tidak valid', $bagian, $subbagian);
                }
            }
            else{
                $page = 'page/error/cant_edit.php';
            }
            break;
        
        case 'entry':
            if( isCanEntry() ){
                $page = sprintf('page/entry/%s/%s.php', $bagian, $subbagian);
            }
            else{
                $page = 'page/error/cant_entry.php';
            }
            
            break;
        
        case 'confirm':
            if( isCanEntry() ){
                $page = sprintf("page/confirm/%s/%s.php", $bagian, $subbagian);
            }
            else{
                $page = 'page/error/cant_entry.php';
            }
            break;
        
        case 'confirm_edit':
            if( isCanEdit() ){
                $page = sprintf("page/confirm/%s/%s.php", $bagian, $subbagian);
            }
            else{
                $page = 'page/error/cant_edit.php';
            }
            break;
     
        case 'confirmed':
            // check mode
            if( isset($_SESSION['edit_id']) && $_SESSION['edit_id'] != NULL ){
                // mode: edit
                if( isCanEdit() ){
                    switch($model->editById($_POST, $_SESSION['edit_id'])){
                        case EDIT_OK:
                            refresh('Update berhasil', $bagian, $subbagian);
                            break;
                        case EDIT_ID_INVALID:
                            refresh('Update batal. Id tidak valid', $bagian, $subbagian);
                            break;
                        default:
                            refresh('Update batal. Hubungi administrator', $bagian, $subbagian);
                    }
                }
                else{
                    $page = 'page/error/cant_edit.php';
                }                
            }
            else{
                // mode: insert
                if( isCanEntry() ){
                    switch($model->put($_POST)){
                        case INSERT_FAIL_DUPLICATE:
                            refresh('Data sudah ada ! Insert Batal.', $bagian, $subbagian);
                            break;
                        case INSERT_FAIL_FORMAT:
                            refresh('Format Input tidak sesuai.<br>Insert dibatalkan.', $bagian, $subbagian);
                            break;
                        case UPDATE_OK:
                            refresh('Update berhasil', $bagian, $subbagian);
                            break;
                        case UPDATE_FAIL:
                            refresh('Update batal, data tidak ditemukan', $bagian, $subbagian);
                            break;
                        default:
                            refresh('Insert berhasil', $bagian, $subbagian);
                    }
                } 
                else{
                    $page = 'page/error/cant_entry.php';
                }
            }
            break;
        default:
            
            if( isCanView() ){
                $page = sprintf('page/view/%s/%s.php', $bagian, $subbagian);
            }
            else{
                $page = 'page/error/cant_view.php';
            }
    }
     
    include 'banner.php';
    echo '<div id="konten">';
    include $page;
	echo '</div>';   
    include 'footer.php';
}
else{
    header('location:menu.php');
}
?>